<template>
  <div>
    <div style="margin-bottom: 10px; font-size: 14px; font-weight: 250; margin-left: 10px">
      <el-icon><SetUp /></el-icon>
      成绩录入
    </div>
    <el-card style>
      <div style="display: flex">
        <el-input placeholder="请输入课程名称" v-model="data.courseName" style="width: 300px; margin-left: 760px;"  prefix-icon="Search"/>
        <el-input placeholder="请输入学生姓名" v-model="data.studentName" style="width: 300px; margin: 0px 20px 0px 20px" prefix-icon="Search"/>
        <el-button type="primary" size="large" @click="load">搜索</el-button>
      </div>
      <el-table :data="data.tableData" stripe style="width: 100%">
        <el-table-column prop="id" label="选课ID" />
        <el-table-column prop="courseName" label="课程名" />
        <el-table-column prop="studentName" label="学生姓名" />
        <el-table-column prop="dailyScore" label="日常分" />
        <el-table-column prop="examScore" label="期末成绩" />
        <el-table-column prop="score" label="成绩" />
        <el-table-column label="操作">
          <template #default="scope">
            <el-button @click="setGrade(scope.row)" type="success">
              打分
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div style="display: flex; justify-content: center; margin-top: 10px">
        <el-pagination layout="prev, pager, next" :total="data.tableData.length" :page-size="10" />
      </div>
    </el-card>
    <el-dialog v-model="data.dialogFormVisible" title="编辑" width="500">
      <el-form :model="data.form" label-width="80px" style="padding: 20px">
        <el-form-item label="选课ID" prop="id">
          <el-input size="large" v-model="data.form.id" disabled></el-input>
        </el-form-item>
        <el-form-item label="日常分" prop="dailyScore">
          <el-input size="large" v-model="data.form.dailyScore" type="number" :min="0" :max="100"></el-input>
        </el-form-item>
        <el-form-item label="期末成绩" prop="examScore">
          <el-input size="large" v-model="data.form.examScore" type="number" :min="0" :max="100"></el-input>
        </el-form-item>
        <el-form-item label="总成绩" prop="score">
          <el-input size="large" v-model="data.form.score" type="number"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <div>
          <el-button @click="data.dialogFormVisible = false" size="large">取消</el-button>
          <el-button type="primary" @click="handleGrade" size="large">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>

import {reactive} from "vue";
import request from "@/utils/request.js";
import {ElMessage} from "element-plus";

const data = reactive({
  tableData: [],
  courseName: null,
  studentName: null,
  dialogFormVisible: false,
  form: {},
})

const teacher = JSON.parse(localStorage.getItem("teacher"))

const load = () => {
  request.get("teacher/selectAllGrade", {
    params: {
      courseName: data.courseName || '',
      studentName: data.studentName || '',
      teacherId: teacher.id
    }}).then(res => {
    data.tableData = res.data
  })
}

load()

const setGrade = (row) => {
  data.form = JSON.parse(JSON.stringify(row))  // 深拷贝
  data.dialogFormVisible = true
}

const handleGrade = () => {
  request.post("teacher/updateGrade", data.form).then(res => {
    if (res.code === "200") {
      ElMessage.success("打分成功")
      load()
      data.dialogFormVisible = false
    } else {
      ElMessage.error(res.msg)
    }
  })
}
</script>